<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Vip;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use App\Admin\Actions\Post\BatchRestore;

class VipController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Vip(), function (Grid $grid) {
            $grid->column('vip_id')->sortable();
            $grid->column('vip_name');
            $grid->column('vip_day');
            $grid->column('price');
            $grid->column('preferential_price');
            $grid->column('sort');
            $grid->column('is_state')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('vip_id');
// 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Vip::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Vip::class));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Vip(), function (Show $show) {
            $show->field('vip_id');
            $show->field('vip_name');
            $show->field('vip_day');
            $show->field('price');
            $show->field('preferential_price');
            $show->field('sort');
            $show->field('is_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Vip(), function (Form $form) {
            $form->display('vip_id');
            $form->text('vip_name');
            $form->text('vip_day');
            $form->text('price');
            $form->text('preferential_price');
            $form->text('sort');
            $form->switch('is_state')->customFormat(function ($v) {
                return $v == '打开' ? 1 : 0;
            })->saving(function ($v) {
                    return $v ? 1 : 0;
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
